package com.jy.wms.common.base;


import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SpringUtil;
import com.jy.wms.dao.WmsShipmentOrderMapper;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsOwnerShipmentorderconfig;
import org.jeecgframework.poi.handler.impl.ExcelDataHandlerDefaultImpl;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;


public class ShipmentOrderMapImportHanlder extends ExcelDataHandlerDefaultImpl {
    WmsShipmentOrderMapper wmsShipmentOrderMapper = SpringUtil.getBean(WmsShipmentOrderMapper.class);

    @Override
    public void setMapValue(Map<String, Object> map, String originKey, Object value) {
        System.out.print("value:" + value);
        /*if (value instanceof Double) {
            map.put(getRealKey(originKey), PoiPublicUtil.doubleToString((Double) value));
        } else {
            map.put(getRealKey(originKey), value != null ? value.toString() : null);
        }*/

        if (value instanceof Double) {
            NumberFormat nf = new DecimalFormat("#");
            value = nf.format(value);
        }
        map.put(getRealKey(originKey), value != null ? value.toString() : null);


    }

    private String getRealKey(String originKey) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }

        System.out.println("originKey:"+originKey);

        WmsOwnerShipmentorderconfig wmsOwnerShipmentorderconfig = wmsShipmentOrderMapper.getWmsShipmentOrderConfig(hzid);

        if (wmsOwnerShipmentorderconfig != null) {
            if (originKey.equals(wmsOwnerShipmentorderconfig.getDh())) {
                return "dh";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getLx())) {
                return "lx";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getShdz())) {
                return "shdz";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getKhmc())) {
                return "khmc";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getKhbh())) {
                return "khbh";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getHpbh())) {
                return "hpbh";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getHpsl())) {
                return "hpsl";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getHplsje())) {
                return "hplsje";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getDdrq())) {
                return "ddrq";
            }
            if (originKey.equals(wmsOwnerShipmentorderconfig.getFhrq())) {
                return "fhrq";
            }
        }
        return originKey;
    }
}
